<template>
  <div class='xtx-goods-page' v-if="goods.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="'/category/'+goods.categories[1].id">{{goods.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem :to="'/category/sub/'+goods.categories[0].id">{{goods.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem to="/">{{goods.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 商品信息左边 -->
        <div class="goods-info-left">
            <GoodsImage :images='goods.mainPictures'></GoodsImage>
            <GoodsSales></GoodsSales>
        </div>
        <!-- 商品信息右边 -->
        <div class="goods-info-right">
            <GoodsName :goods='goods' ></GoodsName>
              <!-- 规格组件 -->
              <GoodsSku :goods='goods' :skuId="'1369155872197971970'" @change='changeFn'></GoodsSku>
              <!-- 数量组件 -->
              <XtxNumbox @changeNumFn="changeNumFn" label="数量"  v-model="count" ></XtxNumbox>
              <!-- 按钮组件 -->
              <XtxButton @click="addToCartBtn" type="primary" style="margin-top:20px;">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id"/>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs></GoodsTabs>

          <!-- 注意事项 -->
          <GoodsWarn></GoodsWarn>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <!-- 1代表24小时热销榜 2代表周热销榜 3代表总热销榜 -->
          <GoodsHot :goodsId='goods.id' :type="1"></GoodsHot>
          <GoodsHot :goodsId='goods.id' :type="2"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/product'
import { useRoute } from 'vue-router'
import { watch, ref, provide } from 'vue'
import GoodsRelevant from './components/goods-relevant.vue'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import XtxNumbox from '@/components/library/xtx-numbox.vue'
import XtxButton from '@/components/library/xtx-button.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
import Message from '@/components/library/Message'
import { useStore } from 'vuex'
export default {
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsName, GoodsSku, XtxNumbox, XtxButton, GoodsTabs, GoodsHot, GoodsWarn },
  name: 'XtxGoodsPage',
  setup () {
    const route = useRoute()
    const store = useStore()
    const goods = ref({})
    // 监听路由发生变化
    watch(() => route.params.id, async (id) => {
      if (route.path !== `/product/${id}`) return
      const { result } = await findGoods(id)
      console.log(result)
      goods.value = result
    }, { immediate: true })
    //    数量组件
    const count = ref(1)

    // 详情组件
    provide('goods', goods)
    // sku组件的更新
    const changeFn = (obj) => {
      if (obj.skuId) {
        goods.value.price = obj.price
        goods.value.oldPrice = obj.oldPrice
        goods.value.inventory = obj.inventory
        goods.value.attrsText = obj.specsText
        // 存储当前 skuId
        goods.value.currSkuId = obj.skuId
      } else {
        // 没有选择完整规格就重置为空
        goods.value.currSkuId = ''
      }
    }
    // 点击数量框
    const changeNumFn = (val) => {
      console.log(val)
      goods.value.count = val
    }
    // 点击添加购物车
    const addToCartBtn = () => {
      // 判断是否选择了完整规格
      if (!goods.value.currSkuId) return Message({ type: 'warn', message: '请选择完整规格' })
      // 库存不足提示
      if (count.value > goods.value.inventory) return Message({ type: 'warn', message: '库存不足了' })
      // 约定本地存储的信息内容 - 字段名可参考购物车列表接口的字段名
      const cartItem = {
        id: goods.value.id,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        price: goods.value.price,
        skuId: goods.value.currSkuId,
        attrsText: goods.value.attrsText,
        nowPrice: goods.value.price,
        stock: goods.value.inventory || goods.value.stock, // 库存字段兼容性写法(库存在不同接口字段名有差别)
        count: count.value,
        selected: true,
        isCollect: false,
        isEffective: true,
        discount: 1
      }
      console.log(goods, cartItem)
      // 添加到 VueX 仓库中，VueX 数据做了本地存储同步，所以可以通过本地存储查看数据
      // store.commit('cart/insertCart', cartItem)
      store.dispatch('cart/insertCart', cartItem).then(res => {
        Message({ type: 'success', message: '加入购物车成功' })
      })
      // 编写mutaions添加购物车逻辑 - 修改state的函数
      // 编写actions进行添加操作 - 考虑到后期需要异步修改，所以再封装 actions
      // 在商品详情页实现添加逻辑
    }
    return { goods, count, changeFn, addToCartBtn, changeNumFn }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .goods-info-left {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .goods-info-right {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
